package com.team.panicgame;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

import aurelienribon.tweenengine.Tween;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import com.team.panicgame.screens.FirstScreen;
import com.team.panicgame.screens.gareve.GareveScreen;
import com.team.panicgame.screens.mauricio.MauricioScreen;
import com.team.panicgame.screens.vudduu.VudduuScreen;
import com.team.panicgame.tweens.BasicTweenAccessor;

public class PanicGame extends Game {
	private static final String TAG = PanicGame.class.getSimpleName();
	
	@Override
	public void create() {
		Gdx.app.setLogLevel(Application.LOG_DEBUG);
		Tween.registerAccessor(Rectangle.class, new BasicTweenAccessor());
		
		loadDefaultPreferences();
		loadPropertiesFile();
		
		String actualUser = Gdx.app.getPreferences("game").getString("actual_user");
		if(actualUser.contains("gareve"))
			setScreen(new GareveScreen(this));
		else if (actualUser.contains("mauricio"))
			setScreen(new MauricioScreen(this));			
		else if (actualUser.contains("vudduu")) {
			setScreen(new VudduuScreen(this));
		} else
			setScreen(new FirstScreen(this));
	}

	private void loadDefaultPreferences(){
		Gdx.app.getPreferences("game").putString("actual_user", "android");
	}

	private void loadPropertiesFile(){
		Properties prop = new Properties();
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		String propertiesPath = String.format("%s/game.properties", System.getProperty("user.home"));
		Gdx.app.log(TAG,String.format("Properties Path = [%s]",propertiesPath));
		
		try {
			InputStream stream = new FileInputStream(propertiesPath);
			prop.load(stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for (Entry<Object, Object> entry : prop.entrySet()) {
			//Gdx.app.log(TAG,String.format("%s = [%s]",entry.getKey(),entry.getValue()));
			Gdx.app.getPreferences("game").putString((String)entry.getKey(), (String)entry.getValue());
		}
		Gdx.app.getPreferences("game").flush();
		
		Gdx.app.log(TAG,"Game Properties");
		for(Entry entry : Gdx.app.getPreferences("game").get().entrySet()){
			Gdx.app.log(TAG,String.format("[%s] = [%s]",entry.getKey(),entry.getValue()));
		}
	}
}
